<?php
namespace app\beautiful\model\partner;
use app\common\model\CommonModel;
use app\beautiful\model\member\MemberModel;

class PartnerCommissionModel extends CommonModel{
    protected  $name = 'beautiful_partner_commission';
    protected  $pk   = 'commission_id';

    public function member()
    {
        return $this->hasOne(MemberModel::class,'member_id','member_id');
    }

    public function getMonetarySumByPartnerId($memberIds){
        $where = [
            ['member_id','IN',$memberIds],
        ];
        $datas = $this->field('member_id,SUM(monetary) AS money')->where($where)->group('member_id')->select();
        $return = [];
        foreach($datas as $val){
            $return[$val['member_id']] = $val['money'];
        }
        return $return;
    }
    
    public function getSubordinateMonetarySumByPartnerId($member_id,$shop_id){
        $memberModel=new MemberModel();
        // 下级客户消费金额
        $sub =[
            ['shop_id','=',$shop_id],
            ['pid','=',$member_id],
        ];
        $sub_member = $memberModel->where($sub)->select()->toArray();
        $sub_member_ids = array_column($sub_member,'member_id');
        $where = [
            ['member_id','IN',$sub_member_ids],
        ];

        $datas = $this->field('member_id,SUM(monetary) AS money')->where($where)->group('member_id')->select()->toArray();
        $money  = 0;
        foreach($datas as $val){
            $money += $val['money'];
        }
        // $return[$member_id] = $money;
        // var_dump($return);exit;
        return $money;
    }
}
